//
//  DestDetailViewController.m
//  安好
//
//  Created by 冯凯 on 14-12-3.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "DestDetailViewController.h"
#import "IllnessTableViewCell.h"
#import "IllnessViewController.h"
#import "PopoverView.h"
@interface DestDetailViewController ()

@end

@implementation DestDetailViewController
{
    NSArray *titles;
    NSArray *images;
    NSArray *selectArray;
    PopoverView *pop;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    titles = @[@"收藏", @"分享"];
    images = @[@"百科_收藏", @"百科_分享"];
    selectArray=@[@"百科_收藏成功", @"百科_分享成功"];

    [self makeNavBar];
    [self viewWithTableView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self viewWithData];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [pop dismiss:YES];
}
- (void)viewWithTableView
{
    self.hosTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight - iOS7TopMargin) style:UITableViewStylePlain];
    self.hosTableView.delegate = self;
    self.hosTableView.dataSource = self;
    self.hosTableView.backgroundColor=[ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];

    self.hosTableView.showsHorizontalScrollIndicator = NO;
    self.hosTableView.showsVerticalScrollIndicator = NO;
    [self.hosTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//去掉系统的分割线
    [self.view addSubview:self.hosTableView];
    
}
-(void)makeNavBar
{
    self.navigationItem.title = self.hosname;
    // “返回”
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBackDown)];
    backBtnItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnItem;
    // “筛选”
    UIButton *rightButtonMore = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButtonMore.frame = CGRectMake(0,0, 23, 23);
    [rightButtonMore setImage:[UIImage imageNamed:@"公用_更多.png"] forState:UIControlStateNormal];
    [rightButtonMore addTarget:self action:@selector(showListView:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightButtonMore];
}
//筛选
-(void)showListView:(UIButton *)sender
{
    CGPoint point = CGPointMake(sender.frame.origin.x + sender.frame.size.width/2, 64);
//    NSArray *titles = @[@"收藏", @"分享"];
    pop = [[PopoverView alloc] initWithPoint:point titles:titles images:images selectImage:selectArray];
    pop.selectRowAtIndex = ^(NSInteger index){
        MyNSLog("select index:%d", index);
        switch (index) {
            case 0:
            {
                if ([[titles objectAtIndex:0] isEqualToString:@"收藏"]) {
                    [self collection];
                }
                else
                {
                    [self deleteCollection];
                }
            }
                break;
            case 1:
            {
                [self showShareView];
            }
                break;
                
            default:
                break;
        }
    };
    [pop show];
}
#pragma 实现share方法，通过点击的视图的tag值判断是什么类型的分享
- (void)share:(UITapGestureRecognizer *)shareTap{
    NSString * url= [NSString stringWithFormat:@"http://m.niruoanhao.com/share/department/%@",self.hoskid];
    NSString * title = @"安好，科普各科室常见疾病，引导挂号与自查";
    NSString * text = @"各科室治疗疾病描述，为分诊挂号提供参考，避免挂错科室";
    UIImage * image = [UIImage imageNamed:@"二维码"];
    switch (shareTap.view.tag) {
        case 11:{//微信
            [self weixinShare:url :title :text :image];
        }
            break;
        case 12:{//朋友圈
            [self weixinFriendShare:url :title :text :image];
        }
            break;
        case 13:{//短信
            [self showSMSPicker:url :title :text :nil];
        }
            break;
        case 14:{//QQ
            [self qqShare:url :title :text :image];
        }
            break;
        case 15:{//QQ空间
            [self qqZoneShare:url :title :text :image];
        }
            break;
        case 16:{//新浪微博
            [self sinaWeiboShare:url :title :text :image];
        }
            break;
            
        default:
            break;
    }
}

- (void)Login
{
    NSString *postURL=[NSString stringWithFormat:@"%@collection/addCollection",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *dict=@{
                         @"platform":kPlatform,
                         @"version":kVersion,
                         @"title":self.navigationItem.title,
                         @"objectid":self.hoskid,
                         @"type":@"3",
                         };
    [manager POST:postURL parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
        }
        else{
            [self showCustomHudSingleLine:@"收藏成功"];
            titles = @[@"已收藏", @"分享"];
            images = @[@"百科_收藏成功", @"百科_分享"];
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
 
}
/**
 *  添加收藏，收藏成功后，把“收藏”置为“已收藏”
 */
- (void)collection
{
    [self isLogin];
}
- (void)deleteCollection
{
    [self showActionView];
    NSString *postURL=[NSString stringWithFormat:@"%@collection/delete/",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *dict=@{
                         @"platform":kPlatform,
                         @"version":kVersion,
                         @"objectid":self.hoskid,
                         @"type":@"3",
                         };
    [manager POST:postURL parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        [self hiddenActionView];
        if ([statusString isEqualToString:@"0"]) {
        }
        else{
            [self showCustomHudSingleLine:@"取消收藏"];
            titles = @[@"收藏", @"分享"];
            images = @[@"百科_收藏", @"百科_分享"];

        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
}

//返回 百科页面
-(void)leftBackDown
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWithData
{
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setObject:self.hoskid forKey:@"departmentid"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@wiki/departmentDetailInfo/",kBaseURL] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        [self hiddenActionView];
        if ([statusString isEqualToString:@"0"]) {
            
        }else{
            self.hosDic = [NSDictionary dictionaryWithDictionary:responseObject];
            NSString *iscollection=[NSString stringWithFormat:@"%@",[[self.hosDic valueForKey:@"data"] valueForKey:@"iscollection"]];
            
            if ([iscollection isEqualToString:@"0"]) {
                titles = @[@"收藏", @"分享"];
                images = @[@"百科_收藏", @"百科_分享"];

            }else
            {
                titles = @[@"已收藏", @"分享"];
                images = @[@"百科_收藏成功", @"百科_分享"];

            }
            self.isload = YES;
            [self.hosTableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 2中cell的加载
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isload == YES) {
        if (section == 0){
            return 1;
        }else{
            if (self.isBaike) {
                return 2;
            }else{
            return 3;
            }
        }
    }else{
        return 0;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString * identifier = @"myCell";
        
        //  检测、查询是否有闲置的单元格
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell == nil){
            cell =  [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        if (indexPath.row == 0) {
            UILabel * cause = [[UILabel alloc]initWithFrame:CGRectMake(15,15, 40, 20)];
            cause.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
            cause.textColor = [UIColor colorWithRed:31.0/255 green:33.0/255 blue:36.0/255 alpha:1];
            cause.text = @"概述";
            [cell addSubview:cause];
            UILabel * causelabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 15,kScreenWidth-15*2 - cause.width , 20) text:[[self.hosDic objectForKeyedSubscript:@"data"] objectForKey:@"introduction"]font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13] textColor:[UIColor darkGrayColor] backgroundColor:[UIColor clearColor] numberOfLines:0 lineSpacing:6];
            [cell addSubview:causelabel];
            UIImageView * dowmImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, causelabel.height + 29, kScreenWidth, 0.5)];
            dowmImageView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#c8c8c8"];
            [cell addSubview:dowmImageView];
            if ([causelabel.text length ] == 0){
                cause.hidden = YES;
                dowmImageView.hidden=YES;
            }
            
        }
        return cell;
    }else{
        IllnessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IllnessTableViewCell"];
        if (cell == nil) {
            if (kScreenWidth == 414) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"IllnessTableViewCell" owner:self options:nil] lastObject];
                
            }else{
                cell = [[[NSBundle mainBundle] loadNibNamed:@"IllnessTableViewCell" owner:self options:nil] firstObject];

            }
        }
        if (self.isBaike) {
            
            if (indexPath.row == 0){
                UIImageView * upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
                upImageView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#c8c8c8"];
                [cell addSubview:upImageView];
                cell.userLabel.text = @"常见疾病";
                [cell.userImage setImage:[UIImage imageNamed:@"百科_常见疾病"]];
                cell.detailLabel.text = [NSString stringWithFormat:@"%@个",[[self.hosDic objectForKey:@"data"] objectForKey:@"commondiseasecount"]];
            }else{
                UIImageView * upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, kScreenWidth - 15, 0.5)];
                upImageView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#c8c8c8"];
                [cell addSubview:upImageView];
                UIImageView * downImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 45, kScreenWidth, 0.5)];
                downImageView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#c8c8c8"];
                [cell addSubview:downImageView];
                cell.userLabel.text = @"所有疾病";
                [cell.userImage setImage:[UIImage imageNamed:@"百科_所有疾病"]];
                cell.detailLabel.text = [NSString stringWithFormat:@"%@种",[[self.hosDic objectForKey:@"data"] objectForKey:@"diseasecount"]];
            }
        }else{
        if (indexPath.row == 0) {
            UIImageView * upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
            upImageView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#c8c8c8"];
            [cell addSubview:upImageView];
            cell.userLabel.text = @"所属科室";
            [cell.userImage setImage:[UIImage imageNamed:@"百科_所属科室"]];
            cell.detailLabel.text =  [NSString stringWithFormat:@"%@",[[self.hosDic objectForKey:@"data"]objectForKey:@"parentdeptname"]];
        }else if (indexPath.row == 1){
            UIImageView * upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, kScreenWidth - 15, 0.5)];
            upImageView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#c8c8c8"];
            [cell addSubview:upImageView];
            cell.userLabel.text = @"常见疾病";
            [cell.userImage setImage:[UIImage imageNamed:@"百科_常见疾病"]];
            cell.detailLabel.text = [NSString stringWithFormat:@"%@个",[[self.hosDic objectForKey:@"data"] objectForKey:@"commondiseasecount"]];
        }else{
            UIImageView * upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, kScreenWidth - 15, 0.5)];
            upImageView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#c8c8c8"];
            [cell addSubview:upImageView];
            UIImageView * downImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 45, kScreenWidth, 0.5)];
            if (kScreenWidth == 414){
                downImageView.frame = CGRectMake(0, 55.5, kScreenWidth, 0.5);
            }else{
                
            }
            downImageView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#c8c8c8"];
            [cell addSubview:downImageView];
            cell.userLabel.text = @"所有疾病";
            [cell.userImage setImage:[UIImage imageNamed:@"百科_所有疾病"]];
            cell.detailLabel.text = [NSString stringWithFormat:@"%@种",[[self.hosDic objectForKey:@"data"] objectForKey:@"diseasecount"]];
        }
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        UILabel * causelabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 15,kScreenWidth-15*2 - 40 , 20) text:[[self.hosDic objectForKeyedSubscript:@"data"] objectForKey:@"introduction"]font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13] textColor:TITLE_ARTICLE_TEXTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:0 lineSpacing:6];
        if (causelabel.height < 5) {
            return 0;
        }else{
    
            return  causelabel.height + 30;
        }
    }else{
        if (kScreenWidth == 414){
            return 56;
        }else{
            return 46;
            
        }
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//设置段头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.isload == YES) {
        
        if (section == 1) {
            return 10;
        }else{
            return 0;
        }
    }else{
        return 0;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section== 1) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
        view.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
        return view;
    }else{
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
        return view;
    }
}
//设置段头高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.isload == YES) {
        
        if (section == 1) {
            return 10;
        }else{
            return 0;
        }
    }else{
        return 0;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section== 1) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
        view.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
        return view;
    }else{
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
        return view;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isBaike) {
        if (indexPath.section == 1){
            if (indexPath.row == 0) {
               
            }else{
                IllnessViewController * doctorViewController = [[IllnessViewController alloc]init];
                doctorViewController. hosid = self.hoskid;
                doctorViewController.isFromBaike = YES;
                [self.navigationController pushViewController:doctorViewController animated:YES];
            }
        }
    }else{
    if (indexPath.section == 1){
        if (indexPath.row == 0) {
            DestDetailViewController *encyDrugMedicineVC = [[DestDetailViewController alloc]init];
            encyDrugMedicineVC. hoskid = TextString([[self.hosDic objectForKeyedSubscript:@"data"] objectForKey:@"parentdeptid"]);
            encyDrugMedicineVC.hosname = TextString([[self.hosDic objectForKeyedSubscript:@"data"] objectForKey:@"parentdeptname"]);
            encyDrugMedicineVC.isBaike = YES;
            [self.navigationController pushViewController:encyDrugMedicineVC animated:YES];
        }else if (indexPath.row == 1){
//            HospitalViewController *encyHosgMedicineVC = [[HospitalViewController alloc]init];
//            encyHosgMedicineVC.hosid = TextString([[self.diseaseDic objectForKeyedSubscript:@"data"] objectForKey:@"diseaseid"]);
//            encyHosgMedicineVC.hosname = TextString([[self.diseaseDic objectForKeyedSubscript:@"data"] objectForKey:@"diseasename"]);
//            encyHosgMedicineVC.isFromBaike = YES;
//            [self.navigationController pushViewController:encyHosgMedicineVC animated:YES];
        }else{
            IllnessViewController * doctorViewController = [[IllnessViewController alloc]init];
            doctorViewController. hosid = self.hoskid;
//            doctorViewController.hosname = TextString([[self.deskDic objectForKeyedSubscript:@"data"] objectForKey:@"parentdeptname"]);
            doctorViewController.isFromBaike = YES;
            [self.navigationController pushViewController:doctorViewController animated:YES];
        }
    }
  }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
